﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;

namespace Lyon.Utility
{
    /// <summary>
    /// 应用程序图标。
    /// </summary>
    static public class AppIcon
    {
        /// <summary>
        /// 获取应用程序图标。
        /// </summary>
        /// <param name="fileName">应用程序文件名</param>
        /// <returns></returns>
        static public Icon GetAppLargeIcon(string fileName)
        {
            SHFILEINFO shinfo = new SHFILEINFO();
            Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
            return Icon.FromHandle(shinfo.hIcon);
        }
        /// <summary>
        /// 获取应用程序图标。
        /// </summary>
        /// <param name="fileName">应用程序文件名</param>
        /// <returns></returns>
        static public Icon GetAppSmallIcon(string fileName)
        {
            SHFILEINFO shinfo = new SHFILEINFO();
            Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
            return Icon.FromHandle(shinfo.hIcon);
        }

        /// <summary>
        /// 获取文件系统图标。
        /// </summary>
        /// <param name="extension">文件扩展名</param>
        /// <returns></returns>
        static public Icon GetLargeIcon(string extension)
        {
            //Icon icon = null;
            //FileInfo info = new FileInfo(String.Format("icon.{0}", extension));
            //using (FileStream fileStream = info.Create())
            //{
            //    icon = GetAppLargeIcon(info.FullName);
            //}
            //info.Delete();
            //return icon;
            SHFILEINFO shinfo = new SHFILEINFO();
            Win32.SHGetFileInfo(extension, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON | Win32.SHGFI_USEFILEATTRIBUTES);
            return Icon.FromHandle(shinfo.hIcon);
        }
        /// <summary>
        /// 获取文件系统图标。
        /// </summary>
        /// <param name="extension">文件扩展名</param>
        /// <returns></returns>
        static public Icon GetSmallIcon(string extension)
        {
            //Icon icon = null;
            //FileInfo info = new FileInfo(String.Format("icon.{0}", extension));
            //using (FileStream fileStream = info.Create())
            //{
            //    icon = GetAppSmallIcon(info.FullName);
            //}
            //info.Delete();
            //return icon;
            SHFILEINFO shinfo = new SHFILEINFO();
            Win32.SHGetFileInfo(extension, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_USEFILEATTRIBUTES);
            return Icon.FromHandle(shinfo.hIcon);
        }

        [StructLayout(LayoutKind.Sequential)]
        struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        };
        class Win32
        {
            public const uint SHGFI_ICON = 0x100;
            public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
            public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
            public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
            [DllImport("shell32.dll")]
            public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
            [DllImport("shell32.dll")]
            public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
        }
    }
}
